<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          src="../assets/logo.png"
          alt=""
        >
        <span>电商后台管理系统</span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div
          class="toggle-button"
          @click="toggleCollapse"
        >|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          :collapse="isCollapse"
          :unique-opened="true"
          background-color="#333744"
          text-color="#fefefe"
          active-text-color="#409EFF"
          :collapse-transition="false"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <!-- index需要是字符串 :index="item1.id + ''" -->
          <el-submenu
            v-for="item1 in menulist"
            :index="item1.path"
            :key="item1.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item1.id]"></i>
              <!-- 文本 -->
              <span>{{item1.authName}}</span>
            </template>
            <!-- 二级菜单列表 -->
            <el-menu-item
              v-for="item2 in item1.children"
              :index="item2.path"
              :key="item2.id"
              @click="saveNavSatate(item2.path)"
            >
              <i class="el-icon-menu"></i>
              {{item2.authName}}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data() {
    return {
      //左侧菜单
      menulist: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      //是否折叠
      isCollapse: false,
      // 选中状态地址
      activePath: ''
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus");
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    //点击按钮切换菜单折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 菜单点击选中状态
    saveNavSatate(path) {
      // console.log(path);
      // 写入会话，刷新还在
      window.sessionStorage.setItem('activePath', path);
      // 设置当前页面点击的
      this.activePath = path;
      this.$router.push('/'+path);
    }
  },
};
</script>

<style lang="stylus" scoped>
.home-container {
  height: 100%;

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fefefe;
    font-size: 18px;

    >div {
      color: #fefefe;
      display: flex;
      align-items: center;

      img {
        width: 50px;
        height: auto;
        margin-right: 10px;
      }
    }
  }

  .el-container {
    .el-aside {
      background-color: #333744;
      color: #fefefe;
      transition: 0.3s;

      .toggle-button {
        background-color: #4a5064;
        text-align: center;
        font-size: 10px;
        line-height: 24px;
        letter-spacing: 0.2em;
        cursor: pointer;
      }

      .el-menu {
        border-right: none;
      }
    }

    .el-main {
      background-color: #eaedf1;
    }
  }
}

.iconfont {
  margin-right: 10px;
}
</style>